﻿Imports Cognex.VisionPro.ToolGroup
Imports Cognex.VisionPro.PMAlign
Imports Cognex.VisionPro.CalibFix
Imports Cognex.VisionPro
Imports System.Math
Imports System.IO

Public Class FrmSetting_2
    Dim Flag As Integer
    Private Sub Button_VisionMark1_Click(sender As Object, e As EventArgs) Handles Button_VisionMark1.Click
        Flag = 1
        If CogVisionControl1.RunFlag = True Then     '判断相机是否在运行中
            MsgBox("相机运行中，请关闭相机")
        Else
            'CogVisionControl1.SetTBCheck(gTBCheck(0), "CogCalibCheckerboardTool1", "CogPMAlignTool1")
            Button_VisionMark1.BackColor = Color.Blue
            Button_VisionMark2.BackColor = Color.White
            Button_VisionMark_SN.BackColor = Color.White
            Button_VisionMark3.BackColor = Color.White
            Button_VisionMark4.BackColor = Color.White
            Button_VisionMark5.BackColor = Color.White

            'CogToolGroupEditV21.Visible = False
            'CogVisionControl1.Visible = True
            CogVisionControl1.Visible = False
            CogToolGroupEditV21.Subject = gTBCheck(0)
            CogToolGroupEditV21.Visible = True
            GroupBox1.Visible = False
        End If
    End Sub

    Private Sub Button_VisionMark2_Click(sender As Object, e As EventArgs) Handles Button_VisionMark2.Click
        Flag = 2
        If CogVisionControl1.RunFlag = True Then     '判断相机是否在运行中
            MsgBox("相机运行中，请关闭相机")
        Else
            ' CogVisionControl1.SetTBCheck(gTBCheck(1), "CogCalibCheckerboardTool1", "CogPMAlignTool1")
            Button_VisionMark1.BackColor = Color.White
            Button_VisionMark2.BackColor = Color.Blue
            Button_VisionMark_SN.BackColor = Color.White
            Button_VisionMark3.BackColor = Color.White
            Button_VisionMark4.BackColor = Color.White
            Button_VisionMark5.BackColor = Color.White

            'CogToolGroupEditV21.Visible = False
            'CogVisionControl1.Visible = True
            CogVisionControl1.Visible = False
            CogToolGroupEditV21.Subject = gTBCheck(1)
            CogToolGroupEditV21.Visible = True
            GroupBox1.Visible = False
        End If
    End Sub

    Private Sub Button_VisionMark_SN_Click(sender As Object, e As EventArgs) Handles Button_VisionMark_SN.Click
        Flag = 6
        If CogVisionControl1.RunFlag = True Then     '判断相机是否在运行中
            MsgBox("相机运行中，请关闭相机")
        Else
            'GetRobotStation()
            Button_VisionMark1.BackColor = Color.White
            Button_VisionMark2.BackColor = Color.White
            Button_VisionMark_SN.BackColor = Color.Blue
            Button_VisionMark3.BackColor = Color.White
            Button_VisionMark4.BackColor = Color.White
            Button_VisionMark5.BackColor = Color.White

            CogVisionControl1.Visible = False
            CogToolGroupEditV21.Subject = gTBCheck(5)
            CogToolGroupEditV21.Visible = True
            'GroupBox1.Visible = True
        End If
        
    End Sub

    Private Sub FrmSetting_2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            Flag = 1
            Labe_ChanPin.Text = gCurrProName
            Button_VisionMark1.BackColor = Color.Blue
            Button_VisionMark2.BackColor = Color.White
            Button_VisionMark_SN.BackColor = Color.White
            Button_VisionMark3.BackColor = Color.White
            Button_VisionMark4.BackColor = Color.White
            Button_VisionMark5.BackColor = Color.White

            If gCurrProName <> "" Then
                FrmMain.initPro(gCurrProName)
                FrmMain.InitSetting()
            End If
            CogVisionControl1.Visible = False
            CogToolGroupEditV21.Subject = gTBCheck(0)
            CogToolGroupEditV21.Visible = True
        Catch ex As Exception
            MsgBox(ex.Message & ex.StackTrace)
        End Try

    End Sub

    Private Sub Button_SaveTool1_Click(sender As Object, e As EventArgs) Handles Button_SaveTool1.Click
        Try
            Dim path As String = gProDataPath & "\" & gCurrProName & "\"
            If Directory.Exists(path) = False Then '如果路径不存在,则创建路径
                Directory.CreateDirectory(path)
            End If
            If Flag <> 0 Then
                MsgBox("保存视觉模板" & Flag)
                CogSerializer.SaveObjectToFile(gTBCheck(Flag - 1), path & "TBCheck" & Flag & ".vpp")
            Else
                MsgBox("保存全部模板")
                For j As Integer = 0 To Cam_num - 1
                    CogSerializer.SaveObjectToFile(gTBCheck(j), path & "TBCheck" & (j + 1).ToString & ".vpp")
                Next
            End If
            Button_VisionMark1.BackColor = Color.White
            Button_VisionMark2.BackColor = Color.White
            Button_VisionMark3.BackColor = Color.White
            Button_VisionMark_SN.BackColor = Color.White
            Button_VisionMark4.BackColor = Color.White
            Button_VisionMark5.BackColor = Color.White
            Flag = 0
            MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString & ex.StackTrace.ToString, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub FrmSetting_2_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
        Me.Dispose()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'Dim sql1 As String = ""
        ''Dim DBpath As String = gProDataPath & gCurrProName & "\photo_point.accdb"
        ''Dim cnStr As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " & DBpath & ";Persist Security Info=False;Jet OLEDB:Database Password='1234';"
        'Try

        '    Dim ds As DataSet = New DataSet
        '    Dim sql As String = "select * from photo_point where Product='" & gCurrProName & "'"
        '    ds = New DataSet
        '    ds = SearchMysql(sql)
        '    If ds.Tables(0).Rows.Count > 0 Then
        '        sql1 = " update " & " photo_point " & "set x=" & SMX.Text & ",y=" & SMY.Text & ",z=" & SMZ.Text & ",c=" & SMC.Text & " where PointName=" & "'photo3robot'" & "and Product= '" & gCurrProName & "'"
        '        SearchMysql(sql1)
        '        MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
        '    End If
        'Catch ex As Exception

        'End Try
    End Sub
    'Private Sub GetRobotStation()
    '    Try
    '        Dim sSendData As String = ""
    '        Dim lStatus As Integer
    '        Dim commandCode As Integer
    '        commandCode = 255 '获取点位坐标
    '        sSendData = "1" & Chr(10) & "1"     '直交
    '        lStatus = FrmMain.AxMelfaRxM1.RequestServiceM(lRobotID, commandCode, Len(sSendData), sSendData, 0, 500, 0)
    '        If lStatus = False Then
    '            MsgBox("当前点位获取失败！")
    '        End If
    '    Catch ex As Exception
    '        MsgBox("GetRobotStation:" & ex.Message.ToString)
    '    End Try
    'End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button_VisionMark3.Click
        Flag = 3
        If CogVisionControl1.RunFlag = True Then     '判断相机是否在运行中
            MsgBox("相机运行中，请关闭相机")
        Else
            Button_VisionMark1.BackColor = Color.White
            Button_VisionMark2.BackColor = Color.White
            Button_VisionMark_SN.BackColor = Color.White
            Button_VisionMark3.BackColor = Color.Blue
            Button_VisionMark4.BackColor = Color.White
            Button_VisionMark5.BackColor = Color.White
            CogVisionControl1.Visible = False
            CogToolGroupEditV21.Subject = gTBCheck(2)
            CogToolGroupEditV21.Visible = True
            'GroupBox1.Visible = False
        End If
    End Sub

    Private Sub Button_VisionMark4_Click(sender As Object, e As EventArgs) Handles Button_VisionMark4.Click
        Flag = 4
        If CogVisionControl1.RunFlag = True Then     '判断相机是否在运行中
            MsgBox("相机运行中，请关闭相机")
        Else
            ' CogVisionControl1.SetTBCheck(gTBCheck(1), "CogCalibCheckerboardTool1", "CogPMAlignTool1")
            Button_VisionMark1.BackColor = Color.White
            Button_VisionMark2.BackColor = Color.White
            Button_VisionMark_SN.BackColor = Color.White
            Button_VisionMark3.BackColor = Color.White
            Button_VisionMark4.BackColor = Color.Blue
            Button_VisionMark5.BackColor = Color.White

            'CogToolGroupEditV21.Visible = False
            'CogVisionControl1.Visible = True
            CogVisionControl1.Visible = False
            CogToolGroupEditV21.Subject = gTBCheck(3)
            CogToolGroupEditV21.Visible = True
            'GroupBox1.Visible = False
        End If
    End Sub

    Private Sub Button_VisionMark5_Click(sender As Object, e As EventArgs) Handles Button_VisionMark5.Click
        Flag = 5
        If CogVisionControl1.RunFlag = True Then     '判断相机是否在运行中
            MsgBox("相机运行中，请关闭相机")
        Else
            ' CogVisionControl1.SetTBCheck(gTBCheck(1), "CogCalibCheckerboardTool1", "CogPMAlignTool1")
            Button_VisionMark1.BackColor = Color.White
            Button_VisionMark2.BackColor = Color.White
            Button_VisionMark_SN.BackColor = Color.White
            Button_VisionMark3.BackColor = Color.White
            Button_VisionMark4.BackColor = Color.White
            Button_VisionMark5.BackColor = Color.Blue


            'CogToolGroupEditV21.Visible = False
            'CogVisionControl1.Visible = True
            CogVisionControl1.Visible = False
            CogToolGroupEditV21.Subject = gTBCheck(4)
            CogToolGroupEditV21.Visible = True
            'GroupBox1.Visible = False
        End If
    End Sub
End Class